/**
 *
 */
package com.xscj.score.action;

import java.util.ArrayList;
import java.util.List;

import com.xscj.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.xscj.domain.Course;
import com.xscj.domain.Grade;
import com.xscj.domain.ScoreByGCXT;
import com.xscj.service.CoursePlan;
import com.xscj.service.GradeSetUp;
import com.xscj.service.ScoreService;

/**
 * @author leorain
 * 通过班级编号、课程编号、学期、考试类型
 * 查询某个班级某个学期某次考试某个课程的考试情况
 */
public class ScoreQueryResultFiveAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 7893978704983467001L;

    @Autowired
    @Qualifier("scoreServiceImpl")
    private ScoreService scoreService;//成绩管理业务逻辑处理类

    @Autowired
    @Qualifier("gradeSetUpImpl")
    private GradeSetUp gradeSetUp;//班级管理业务逻辑处理类

    @Autowired
    @Qualifier("coursePlanImpl")
    private CoursePlan coursePlan;

    @Autowired
    @Qualifier("stuServiceImpl")
    private StuService stuService;

    private String gradeID;//班级编号
    private Grade grade;
    private String courseID;//课程编号
    private Course course;
    private int xueqi;//学期
    private String examType;//考试类型;
    private String examTime; //考试时间

    private List<ScoreByGCXT> scoreByGCXTs;

    private double totalScore = 0;//总分
    private int stuCount;//某个班级学生人数
    private int stuExamCount;//某个班级参加考试学生人数
    private double avgScore;//平均分
    private double maxScore;//最高分
    private double minScore;//最低分
    private int failCount = 0;//不及格人数
    private int sucCount = 0;//及格人数
    private int goodCount = 0;//良好人数
    private int secondaryCount = 0;//中等人数
    private int excellentCount = 0;//优秀人数

    public double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(double totalScore) {
        this.totalScore = totalScore;
    }

    public int getStuCount() {
        return stuCount;
    }

    public void setStuCount(int stuCount) {
        this.stuCount = stuCount;
    }

    public int getStuExamCount() {
        return stuExamCount;
    }

    public void setStuExamCount(int stuExamCount) {
        this.stuExamCount = stuExamCount;
    }

    public double getAvgScore() {
        return avgScore;
    }

    public void setAvgScore(double avgScore) {
        this.avgScore = avgScore;
    }

    public double getMaxScore() {
        return maxScore;
    }

    public void setMaxScore(double maxScore) {
        this.maxScore = maxScore;
    }

    public double getMinScore() {
        return minScore;
    }

    public void setMinScore(double minScore) {
        this.minScore = minScore;
    }

    public int getFailCount() {
        return failCount;
    }

    public void setFailCount(int failCount) {
        this.failCount = failCount;
    }

    public int getSucCount() {
        return sucCount;
    }

    public void setSucCount(int sucCount) {
        this.sucCount = sucCount;
    }

    public int getGoodCount() {
        return goodCount;
    }

    public void setGoodCount(int goodCount) {
        this.goodCount = goodCount;
    }

    public int getSecondaryCount() {
        return secondaryCount;
    }

    public void setSecondaryCount(int secondaryCount) {
        this.secondaryCount = secondaryCount;
    }

    public int getExcellentCount() {
        return excellentCount;
    }

    public void setExcellentCount(int excellentCount) {
        this.excellentCount = excellentCount;
    }

    @Override
    public String execute() throws Exception {
        maxScore = 0;
        minScore = course.getScore();
        int fenDuan = course.getScore()/10;
        List<Double> score = new ArrayList<Double>();
        stuExamCount = scoreByGCXTs.size();
        for (int i=0; i<scoreByGCXTs.size();i++) {
            score.add(scoreByGCXTs.get(i).getScore());
        }
        for (int i = 0; i < score.size(); i++) {
            if (score.get(i) >= maxScore)
                maxScore = score.get(i);//求最高分
            if (score.get(i) <= minScore)
                minScore = score.get(i);//求最低分
            totalScore = totalScore + score.get(i);//总分
            int s = (int) (score.get(i) / fenDuan);
            switch (s) {
                case 10:
                case 9:
                    excellentCount++;
                    break;
                case 8:
                    secondaryCount++;
                    break;
                case 7:
                    goodCount++;
                    break;
                case 6:
                    sucCount++;
                    break;
                default:
                    failCount++;
                    break;
            }
        }
        avgScore = Math.round(totalScore / stuExamCount);//平均分
        return SUCCESS;
    }

    @Override
    public void validate() {
        stuCount = stuService.getAllSubStudentsCountByGradeID(gradeID);
        grade = gradeSetUp.getGradeBybianHao(gradeID);
        course = coursePlan.getCourseByCourseID(courseID);
        scoreByGCXTs = scoreService.getscByGCXTs(gradeID, courseID, xueqi, examType);
        if (scoreByGCXTs != null && scoreByGCXTs.size() > 0) {
            examTime = scoreByGCXTs.get(0).getExamTime();
        }
    }

    public String getGradeID() {
        return gradeID;
    }

    public void setGradeID(String gradeID) {
        this.gradeID = gradeID;
    }

    public String getCourseID() {
        return courseID;
    }

    public void setCourseID(String courseID) {
        this.courseID = courseID;
    }

    public int getXueqi() {
        return xueqi;
    }

    public void setXueqi(int xueqi) {
        this.xueqi = xueqi;
    }

    public String getExamType() {
        return examType;
    }

    public void setExamType(String examType) {
        this.examType = examType;
    }

    public String getExamTime() {
        return examTime;
    }

    public void setExamTime(String examTime) {
        this.examTime = examTime;
    }

    public List<ScoreByGCXT> getScoreByGCXTs() {
        return scoreByGCXTs;
    }

    public void setScoreByGCXTs(List<ScoreByGCXT> scoreByGCXTs) {
        this.scoreByGCXTs = scoreByGCXTs;
    }

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

}
